package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyDept;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqylMaterialdetail;
import com.jy.adscm.operation.mscyb.bean.JqylMaterialdetailApprove;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialdetailApproveMapper;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialdetailMapper;
import com.jy.adscm.operation.mscyb.service.JqylMaterialdetailApproveService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.service.JqylMaterialdetailService;
import com.jy.adscm.system.mapper.mysql.JyDeptMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-06-21
 */
@Service
public class JqylMaterialdetailApproveServiceImpl extends ServiceImpl<JqylMaterialdetailApproveMapper, JqylMaterialdetailApprove> implements JqylMaterialdetailApproveService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyDeptMapper jyDeptMapper;
    @Autowired
    JqylMaterialdetailService jqylMaterialdetailService;
    @Override
    public Boolean updateJqylMaterialdetailApprove(JqylMaterialdetailApprove jqylMaterialdetailApprove) throws AdscmSystemException {
       /* boolean b = super.updateById(jqylMaterialdetailApprove);
        //查询这个各个部门的评审意见
        Wrapper<JqylMaterialdetailApprove> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",jqylMaterialdetailApprove.getPid());
        List<JqylMaterialdetailApprove> jqylMaterialdetailApproves = super.selectList(wrapper);
        String result = jqylMaterialdetailApprove.getResult();
        int temp=0;
        for (JqylMaterialdetailApprove materialdetailApprove : jqylMaterialdetailApproves) {
            if (!result.equals(materialdetailApprove.getResult())){
                temp=1;
            }
        }
         if (temp==0){
             JqylMaterialdetail jqylMaterialdetail = jqylMaterialdetailService.selectById(jqylMaterialdetailApprove.getPid());
             jqylMaterialdetail.setResult( Integer.valueOf( jqylMaterialdetailApprove.getResult()));
             jqylMaterialdetailService.updateUnqualified(jqylMaterialdetail);
         }*/
          if (jqylMaterialdetailApprove.getDept()==4){
              if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetailApprove.getResult(),null)){
                  JqylMaterialdetail jqylMaterialdetail = jqylMaterialdetailService.selectById(jqylMaterialdetailApprove.getPid());
                  jqylMaterialdetail.setResult( Integer.valueOf( jqylMaterialdetailApprove.getResult()));
                  jqylMaterialdetailService.updateUnqualified(jqylMaterialdetail);
              }
          }
        boolean b = super.updateById(jqylMaterialdetailApprove);
        return b;
    }

    @Override
    public List<JqylMaterialdetailApprove> selectByPid(String pid) {
        Wrapper<JqylMaterialdetailApprove>  jqylMaterialdetailApproveWrapper =new EntityWrapper<>();
        jqylMaterialdetailApproveWrapper.eq("pid",pid);
        List<JqylMaterialdetailApprove> jqylMaterialdetailApproves = super.selectList(jqylMaterialdetailApproveWrapper);
        for (JqylMaterialdetailApprove jqylMaterialdetailApprove : jqylMaterialdetailApproves) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetailApprove.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqylMaterialdetailApprove.getCreator());
                jqylMaterialdetailApprove.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterialdetailApprove.getDept(),null)){
                JyDept jyDept = jyDeptMapper.selectById(jqylMaterialdetailApprove.getDept());
                jqylMaterialdetailApprove.setDeptName(jyDept.getDeptName());
            }
        }
        return jqylMaterialdetailApproves;
    }

    @Override
    public boolean insertJqylMaterialdetailApprove(JqylMaterialdetail jqylMaterialdetail) {
        int [] depts={4,5,7,8};
        JqylMaterialdetailApprove jqylMaterialdetailApprove =new JqylMaterialdetailApprove();
        jqylMaterialdetailApprove.setPid(jqylMaterialdetail.getId());
        for (int dept : depts) {
            jqylMaterialdetailApprove.setDept(dept);
            super.insert(jqylMaterialdetailApprove);
        }
        return false;
    }
}
